package com.qdxwx.web.controller.ajax;

import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.data.service.book.BookService;
import com.qdxwx.data.service.course.CourseService;
import com.qdxwx.data.service.document.DocumentService;
import com.qdxwx.data.service.favorite.FavoriteService;
import com.qdxwx.data.service.picture.PictureService;
import com.qdxwx.models.book.Book;
import com.qdxwx.models.course.Course;
import com.qdxwx.models.document.Document;
import com.qdxwx.models.favorite.Favorite;
import com.qdxwx.models.picture.Picture;
import com.qdxwx.models.user.User;
import com.qdxwx.web.controller.BaseController;
import com.qdxwx.web.utils.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 个人中心  Controller
 * @author developer
 */
@Controller
@RequestMapping("/ajax/favorite")
public class AjaxFavoriteController extends BaseController {

	@Autowired
	private FavoriteService favoriteService;
	@Autowired
	private CourseService courseService;
	@Autowired
	private BookService bookService;
	@Autowired
	private PictureService pictureService;
	@Autowired
	private DocumentService documentService;

	@RequestMapping("/list.json")
	@ResponseBody
	public AjaxResult List(HttpServletRequest request){
		User user = LoginUser.getUser(request);
		if(user == null) {
			return AjaxResult.error("登录超时");
		}
		int typeId = TKConvert.toInteger(request.getParameter("typeId"),2);
		int page = TKConvert.toInteger(request.getParameter("page"),1);
		int limit = TKConvert.toInteger(request.getParameter("limit"),6);
		PageRequest pageRequest = new PageRequest();
		pageRequest.setCurrentPage(page);
		pageRequest.setPageSize(limit);
		List<Favorite> list = favoriteService.getList(user.getUserId(), typeId, pageRequest);
		return AjaxResult.success(list);
	}

	/**
	 * 删除收藏
	 */
	@RequestMapping("/delete.json")
	@ResponseBody
	public AjaxResult deleteFavorite(HttpServletRequest request) {
		long userId = LoginUser.getUserId(request);
		if (userId < 1) {
			return AjaxResult.auth("登录超时！");
		}
		int infoId = TKConvert.toInteger(request.getParameter("infoId"));
		int id = TKConvert.toInteger(request.getParameter("id"));
		Favorite favorite;
		int row = 0;
		if (id > 0) {
			favorite = favoriteService.get(id);
			if (favorite == null || favorite.getUserId() != userId) {
				return AjaxResult.success("取消收藏失败！");
			}
			row = favoriteService.delete(id);
		} else if (infoId > 0) {
			favorite = new Favorite();
			favorite.setInfoId(infoId);
			favorite.setUserId(userId);
			favorite.setTypeId(TKConvert.toInteger(request.getParameter("typeId")));
			row = favoriteService.delete(favorite);
		}
		if (row > 0) {
			return AjaxResult.success("取消收藏成功");
		} else {
			return AjaxResult.error("取消收藏失败！");
		}
	}

	/**
	 * 添加课程收藏
	 */
	@RequestMapping("/add.json")
	@ResponseBody
	public AjaxResult createFavorite(HttpServletRequest request) {
		long userId = LoginUser.getUserId(request);
		if (userId < 1) return AjaxResult.auth("登录超时");
		int infoId = TKConvert.toInteger(request.getParameter("infoId"));
		if (infoId < 1) return AjaxResult.error("请选择要收藏的内容！");
		int typeId = TKConvert.toInteger(request.getParameter("typeId"));
		if (typeId < -1) return AjaxResult.error("typeId参数异常！");
		Favorite favorite = new Favorite();
		favorite.setInfoId(infoId);
		favorite.setUserId(userId);
		switch (typeId){
			case 2:
				if(favoriteService.isExist(userId, infoId, typeId)){
					return AjaxResult.error("该课程你已经收藏过了！");
				}
				Course course = courseService.get(infoId);
				if(course == null){
					return AjaxResult.error("找不到指定课程！");
				}
				favorite.setTitle(course.getCourseName());
				favorite.setCover(course.getCover());
				favorite.setPrice(course.getPrice());
				favorite.setTypeId(typeId);
				break;
			case 3:
				if(favoriteService.isExist(userId, infoId, typeId)){
					return AjaxResult.error("该书籍你已经收藏过了！");
				}
				Book book = bookService.get(infoId);
				if(book == null){
					return AjaxResult.error("找不到指定书籍！");
				}
				favorite.setTitle(book.getTitle());
				favorite.setCover(book.getCover());
				favorite.setPrice(book.getPrice());
				favorite.setTypeId(typeId);
				break;
			case  4:
				if(favoriteService.isExist(userId, infoId,typeId)){
					return AjaxResult.error("该素材你已经收藏过了！");
				}
				Picture picture = pictureService.get(infoId);
				if(picture == null){
					return AjaxResult.error("找不到指定素材！");
				}
				favorite.setTitle(picture.getTitle());
				favorite.setCover(picture.getCoverUrl());
				favorite.setPrice(picture.getPrice());
				favorite.setTypeId(typeId);
				break;
			case  5:
				if(favoriteService.isExist(userId, infoId,typeId)){
					return AjaxResult.error("该资料你已经收藏过了！");
				}
				Document document = documentService.get(infoId);
				if(document == null){
					return AjaxResult.error("找不到指定素材！");
				}
				favorite.setTitle(document.getTitle());
				favorite.setCover(document.getCover());
				favorite.setPrice(document.getPrice());
				favorite.setTypeId(typeId);
				break;
			default:
				return AjaxResult.error("参数错误！");
		}
		favoriteService.insert(favorite);
		return AjaxResult.success("收藏成功！");
	}
}